﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace CoreLib
{
    public class CDrawableComponent:CDynamicComponent
    {
        private Matrix world = new Matrix();

        public Matrix World
        {
            get { return world; }
            set { world = value; }
        }
        private string assetName;

        public string AssetName
        {
            get { return assetName; }
            set { assetName = value; }
        }
        private string textureName;

        public string TextureName
        {
            get { return textureName; }
            set { textureName = value; }
        }
       public Model model;

        public Model RenderModel
        {
            get { return model; }
            set { model = value; }
        }

        private Game pgame;

        public Game game
        {
            get { return pgame; }
            set { pgame = value; }
        }
        public CDrawableComponent(Game g)
            : base(g)
        {
            game = g;
        }
        public void Load()
        {
            LoadContent();
        }
        protected override void LoadContent()
        {
            try
            {
                if (AssetName == "") return;
                RenderModel = game.Content.Load<Model>(AssetName);
                Transforms = new Matrix[RenderModel.Bones.Count];
                RenderModel.CopyAbsoluteBoneTransformsTo(Transforms);

             
            }
            catch (Exception)
            {

            }

        }
       
        public override void Draw(GameTime gameTime)
        {
            try
            {
                if (RenderModel == null) return;
                RenderModel.CopyAbsoluteBoneTransformsTo(Transforms);
                foreach (ModelMesh mesh in RenderModel.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        World = Transforms[mesh.ParentBone.Index] * PositionMatrix * RotationMatrix * ScaleMatrix;
                        effect.EnableDefaultLighting();
                        effect.World = World;
                        effect.View = Engine.CurrentCamera.View;
                        effect.Projection = CCameraComponent.Projection;
                        effect.PreferPerPixelLighting = true;
                        effect.SpecularPower = 16;
                    }
                    mesh.Draw();

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
    }
}
